package com.example.ams.controller;

import com.example.ams.dto.ShiftSettingDTO;
import com.example.ams.service.ShiftSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 班次设置管理控制器
 * Shift Setting Controller
 */
@RestController
@RequestMapping("/api/shift-setting")
@CrossOrigin(origins = "*")
public class ShiftSettingController {

    @Autowired
    private ShiftSettingService shiftSettingService;

    /**
     * 获取当前生效的班次设置
     * GET /api/shift-setting/current
     */
    @GetMapping("/current")
    public ResponseEntity<Map<String, Object>> getCurrentShift() {
        try {
            ShiftSettingDTO shift = shiftSettingService.getCurrentShift();
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("message", "success");
            response.put("data", shift);
            return ResponseEntity.ok(response);
        } catch (RuntimeException e) {
            Map<String, Object> response = new HashMap<>();
            response.put("code", 404);
            response.put("message", e.getMessage());
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
        }
    }

    /**
     * 更新班次设置
     * PUT /api/shift-setting
     */
    @PutMapping
    public ResponseEntity<Map<String, Object>> updateShift(
            @Validated @RequestBody ShiftSettingDTO shiftDTO) {
        try {
            ShiftSettingDTO updated = shiftSettingService.saveOrUpdate(shiftDTO);
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("message", "班次设置更新成功");
            response.put("data", updated);
            return ResponseEntity.ok(response);
        } catch (RuntimeException e) {
            Map<String, Object> response = new HashMap<>();
            response.put("code", 400);
            response.put("message", e.getMessage());
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response);
        }
    }
}

